﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Npgsql;
using System.Data;

namespace SIGEB.Modelo.moduloComputadoras
{
    public class conjuntoInfoComputadoras : conjuntoBase
    {
        #region constructores

        public conjuntoInfoComputadoras(datosAplicacion dA) : base(dA)
        {
            numTipo = conjuntoBase.CONJINFOCOMPUTADORAS;
        }

        #endregion

        #region metodos

        public override object obtenerEstado()
        {
            throw new NotImplementedException();
        }

        public void agregarInfoComputadora(infoComputadora nuevaInfoComputadora)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT registrar_infocomputadora('" + nuevaInfoComputadora.username + "', '" + nuevaInfoComputadora.encriptar_password() + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al agregar infoComputadora: " + nuevaInfoComputadora.idInfoComp);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public void eliminarInfoComputadora(string idInfoComputadora)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT eliminar_infocomputadora('" + idInfoComputadora + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al eliminar infoComputadora: " + idInfoComputadora);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public void modificarInfoComputadora(string idInfoComputadora, infoComputadora nuevaInfoComputadora)
        {
            try
            {
                compConexion.Open();
                string sql = "SELECT actualizar_infocomputadora('" + idInfoComputadora + "', '" + nuevaInfoComputadora.username + "', '" + nuevaInfoComputadora.password + "')";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al modificar infoComputadora: : " + idInfoComputadora);
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            notificar(this.numTipo, null);
        }

        public LinkedList<infoComputadora> obtenerInfoComputadoras()
        {
            LinkedList<infoComputadora> nueva = new LinkedList<infoComputadora>();
            try
            {
                compConexion.Open();
                string sql = "SELECT * FROM consultar_infocomputadora()";
                NpgsqlDataAdapter da = new NpgsqlDataAdapter(sql, compConexion);
                // i always reset DataSet before i do something with it.... i don't know why :-)
                ds.Reset();
                // filling DataSet with result from NpgsqlDataAdapter
                da.Fill(ds);
                // since it C# DataSet can handle multiple tables, we will select first
                dt = ds.Tables[0];
                string[] valores;
                foreach (DataRow row in dt.Rows)
                {
                    valores = datosDataSet(row, 3);
                    nueva.AddLast(new infoComputadora(valores[0], valores[1], AESEncryption.Decrypt(valores[2], "sigeb_ic_pass")));
                }
            }
            catch (Exception e)
            {
                throw new Exception(e.Message + "\nError al obtener la Informacion de las Computadoras.");
            }
            finally
            {
                try
                {
                    compConexion.Close();
                }
                catch
                {

                }
            }
            return nueva;
        }

        #endregion     
    }
}
